%% Figure4.m
%  This script reproduces Figure 4.
%
% Estimated run time: 5 minutes.


% LOAD THE WEIGHTS FROM THE FILES INTO VARIABLES
clc
clear
close all
nrepetitions=70;
repduration=800;
tic
%% LOAD WEIGHTS FOR SESSION 1

conta=1;
cd('.\SAVED_FILES_Generation_1_Chromosome_3')
for i=repduration/1000:repduration/1000:nrepetitions*repduration/1000
    if mod(i,1)<1e-2
        filename=sprintf('FinalWeights%1.0f.dat',i);
    else
        filename=sprintf('FinalWeights%1.1f.dat',i);
    end
    eval(['FinalWeights' num2str(conta) '= load(filename);'])
    conta = conta+1;
end
cd ..
FinalWeights0=load('WeightsEBCC_3.dat');

total = 0;
for i=1:size(FinalWeights0,1)
    total=total+FinalWeights0(i,1);
end

pesi=zeros(total,nrepetitions+1);
count = 1;
j=0;
eval([ 'M = FinalWeights' num2str(j) ';' ]);
for i=1:size(M,1)
    if M(i,1)==1
           pesi(count,1)=M(i,2);
           count = count+1;
    else
        while(M(i,1)>=1)
                pesi(count,1)=M(i,2);
                M(i,1) = M(i,1)-1;
                count = count+1;
        end
    end
end

k=1;
for j=0:1:nrepetitions
    count = 1;    
    eval([ 'M = FinalWeights' num2str(j) ';' ]);
    for i=1:size(M,1)
        if M(i,1)==1
           pesi(count,k)=M(i,2);
           count = count+1;
        else
            while(M(i,1)>=1)
                pesi(count,k)=M(i,2);
                M(i,1) = M(i,1)-1;
                count = count+1;
            end
        end
    end
    k=k+1;
end

pesi_sani{1}=pesi;

%% LOAD WEIGHTS FOR SESSION2 SHAM

for chromo=0:25

cd(['.\SAVED_FILES_Generation_2_Chromosome_' num2str(chromo)])
conta=1;
for i=repduration/1000:repduration/1000:nrepetitions*repduration/1000
    if mod(i,1)<1e-2
        filename=sprintf('FinalWeights%1.0f.dat',i);
    else
        filename=sprintf('FinalWeights%1.1f.dat',i);
    end
    eval(['FinalWeights' num2str(conta) '= load(filename);'])
    conta = conta+1;
end
FinalWeights0=load('..\WeightsEBCC_9999.dat');

total = 0;
for i=1:size(FinalWeights0,1)
    total=total+FinalWeights0(i,1);
end

pesi=zeros(total,nrepetitions+1);
count = 1;
j=0;
eval([ 'M = FinalWeights' num2str(j) ';' ]);
for i=1:size(M,1)
    if M(i,1)==1
           pesi(count,1)=M(i,2);
           count = count+1;
    else
        while(M(i,1)>=1)
                pesi(count,1)=M(i,2);
                M(i,1) = M(i,1)-1;
                count = count+1;
        end
    end
end

k=1;
for j=0:1:nrepetitions
    count = 1;    
    eval([ 'M = FinalWeights' num2str(j) ';' ]);
    for i=1:size(M,1)
        if M(i,1)==1
           pesi(count,k)=M(i,2);
           count = count+1;
        else
            while(M(i,1)>=1)
                pesi(count,k)=M(i,2);
                M(i,1) = M(i,1)-1;
                count = count+1;
            end
        end
    end
    k=k+1;
end

pesi_sani2{chromo+1}=pesi;
cd ..
end



%% LOAD WEIGHTS FOR SESSION2 TBS

for chromo=0:43

cd(['.\SAVED_FILES_Generation_3_Chromosome_' num2str(chromo)])
conta=1;
for i=repduration/1000:repduration/1000:nrepetitions*repduration/1000
    if mod(i,1)<1e-2
        filename=sprintf('FinalWeights%1.0f.dat',i);
    else
        filename=sprintf('FinalWeights%1.1f.dat',i);
    end
    eval(['FinalWeights' num2str(conta) '= load(filename);'])
    conta = conta+1;
end
FinalWeights0=load('..\WeightsEBCC_9999.dat');

total = 0;
for i=1:size(FinalWeights0,1)
    total=total+FinalWeights0(i,1);
end

pesi=zeros(total,nrepetitions+1);
count = 1;
j=0;
eval([ 'M = FinalWeights' num2str(j) ';' ]);
for i=1:size(M,1)
    if M(i,1)==1
           pesi(count,1)=M(i,2);
           count = count+1;
    else
        while(M(i,1)>=1)
                pesi(count,1)=M(i,2);
                M(i,1) = M(i,1)-1;
                count = count+1;
        end
    end
end

k=1;
for j=0:1:nrepetitions
    count = 1;    
    eval([ 'M = FinalWeights' num2str(j) ';' ]);
    for i=1:size(M,1)
        if M(i,1)==1
           pesi(count,k)=M(i,2);
           count = count+1;
        else
            while(M(i,1)>=1)
                pesi(count,k)=M(i,2);
                M(i,1) = M(i,1)-1;
                count = count+1;
            end
        end
    end
    k=k+1;
end

pesi_tms2{chromo+1}=pesi;
cd ..
end

clearvars -except pesi_sani pesi_sani2 pesi_tms2

%% NOW PLOT THE HISTOGRAMS OF THE WEIGHTS AS IN FIGURE4

count=1;
nrepetitions=70;
repduration=800;
load DataSet.mat

%% Compute the histograms
for rep=1:71
    istogramma_sani(rep,1:20)=hist(pesi_sani{1}(8001:8001+19163,rep),linspace(0,2,20));
    istogramma2_sani(rep,1:20)=hist(pesi_sani{1}(46463:46463+599,rep),linspace(0,0.1,20));
    istogramma3_sani(rep,1:20)=hist(pesi_sani{1}(47663:47663+11,rep),linspace(0,0.2,20));
    I1_s1(rep,1:20)=(istogramma_sani(rep,:,:));
    I2_s1(rep,1:20)=(istogramma2_sani(rep,:,:));
    I3_s1(rep,1:20)=(istogramma3_sani(rep,:,:));    
end

for rep=1:71
    for chromo=1:size(Unici_sani2,1)
        istogramma_sani2(rep,chromo,1:20)=hist(pesi_sani2{chromo}(8001:8001+19163,rep),linspace(0,2,20)).*Numerosita_sani2(chromo);
        istogramma2_sani2(rep,chromo,1:20)=hist(pesi_sani2{chromo}(46463:46463+599,rep),linspace(0,0.1,20)).*Numerosita_sani2(chromo);
        istogramma3_sani2(rep,chromo,1:20)=hist(pesi_sani2{chromo}(47663:47663+11,rep),linspace(0,0.2,20)).*Numerosita_sani2(chromo);
    end
    I1_s2(rep,1:20)=sum(istogramma_sani2(rep,:,:))./588;
    I2_s2(rep,1:20)=sum(istogramma2_sani2(rep,:,:))./588;
    I3_s2(rep,1:20)=sum(istogramma3_sani2(rep,:,:))./588;  
end

for rep=1:71
    for chromo=1:size(Unici_tms2,1)
        istogramma_tms2(rep,chromo,1:20)=hist(pesi_tms2{chromo}(8001:8001+19163,rep),linspace(0,2,20)).*Numerosita_tms2(chromo);
        istogramma2_tms2(rep,chromo,1:20)=hist(pesi_tms2{chromo}(46463:46463+599,rep),linspace(0,0.1,20)).*Numerosita_tms2(chromo);
        istogramma3_tms2(rep,chromo,1:20)=hist(pesi_tms2{chromo}(47663:47663+11,rep),linspace(0,0.2,20)).*Numerosita_tms2(chromo);    
    end
    I1_t2(rep,1:20)=sum(istogramma_tms2(rep,:,:))./753;
    I2_t2(rep,1:20)=sum(istogramma2_tms2(rep,:,:))./753;
    I3_t2(rep,1:20)=sum(istogramma3_tms2(rep,:,:))./753;  
end

%% Draw the Figures

% PF-PC
close all
figureFullScreen
subplot(3,3,1)
bar(linspace(0,2,20),I1_s1(1,:),'facecolor','b','edgecolor','none')
title('Initialization','FontSize',16)
set(gca,'FontSize',16)
box off
xlim([-0.05 2.05])
subplot(3,3,2)
bar(linspace(0,2,20),I1_s1(61,:),'facecolor','b','edgecolor','none')
title('End of Trial 60','FontSize',16)
set(gca,'FontSize',16)
box off
ylim([0 20000])
xlim([-0.05 2.05]) 
subplot(3,3,3)
bar(linspace(0,2,20),I1_s1(70,:),'facecolor','b','edgecolor','none')
title('End of Trial 70','FontSize',16)
set(gca,'FontSize',16)
box off
ylim([0 20000])
xlim([-0.05 2.05]) 


subplot(3,3,5)
bar(linspace(0,2,20),I1_s2(61,:),'edgecolor','none','facecolor','g')
ylim([0 20000])
xlim([-0.05 2.05]) 
title('End of Trial 130','FontSize',16)
set(gca,'FontSize',16)
box off

subplot(3,3,8)
bar(linspace(0,2,20),I1_t2(61,:),'edgecolor','none','facecolor','r')
ylim([0 20000])
xlim([-0.05 2.05]) 
title('End of Trial 130','FontSize',16)
set(gca,'FontSize',16)
box off

subplot(3,3,6)
hold on
bar(linspace(0,2,20),I1_s2(70,:),'edgecolor','none','facecolor','g')
ylim([0 20000])
xlim([-0.05 2.05]) 
title('End of Trial 140','FontSize',16)
set(gca,'FontSize',16)
box off

subplot(3,3,9)
bar(linspace(0,2,20),I1_t2(70,:),'edgecolor','none','facecolor','r')
ylim([0 20000])
xlim([-0.05 2.05]) 
title('End of Trial 130','FontSize',16)
set(gca,'FontSize',16)
box off

suptitle('PF-PC')

% MF-DCN
figureFullScreen
subplot(3,3,1)
bar(linspace(0,0.1,20),I2_s1(1,:),'facecolor','b','edgecolor','none')
title('Initialization','FontSize',16)
set(gca,'FontSize',16)
box off
ylim([0 600])
xlim([0 0.1])
subplot(3,3,2)
bar(linspace(0,0.1,20),I2_s1(61,:),'facecolor','b','edgecolor','none')
title('End of Trial 60','FontSize',16)
set(gca,'FontSize',16)
box off
ylim([0 600])
xlim([0 0.1]) 
subplot(3,3,3)
bar(linspace(0,0.1,20),I2_s1(70,:),'facecolor','b','edgecolor','none')
title('End of Trial 70','FontSize',16)
set(gca,'FontSize',16)
box off
ylim([0 600])
xlim([0 0.1]) 


subplot(3,3,5)
bar(linspace(0,0.1,20),I2_s2(61,:),'edgecolor','none','facecolor','g')
ylim([0 600])
xlim([0 0.1]) 
title('End of Trial 130','FontSize',16)
set(gca,'FontSize',16)
box off

subplot(3,3,8)
bar(linspace(0,0.1,20),I2_t2(61,:),'edgecolor','none','facecolor','r')
ylim([0 600])
xlim([0 0.1]) 
title('End of Trial 130','FontSize',16)
set(gca,'FontSize',16)
box off

subplot(3,3,6)
hold on
bar(linspace(0,0.1,20),I2_s2(70,:),'edgecolor','none','facecolor','g')
ylim([0 600])
xlim([0 0.1]) 
title('End of Trial 140','FontSize',16)
set(gca,'FontSize',16)
box off

subplot(3,3,9)
bar(linspace(0,0.1,20),I2_t2(70,:),'edgecolor','none','facecolor','r')
ylim([0 600])
xlim([0 0.1]) 
title('End of Trial 130','FontSize',16)
set(gca,'FontSize',16)
box off

suptitle('MF-DCN')

% PC-DCN
figureFullScreen
subplot(3,3,1)
bar(linspace(0,0.2,20),I3_s1(1,:),'facecolor','b','edgecolor','none')
title('Initialization','FontSize',16)
set(gca,'FontSize',16)
box off
ylim([0 12])
xlim([0 0.2])
subplot(3,3,2)
bar(linspace(0,0.2,20),I3_s1(61,:),'facecolor','b','edgecolor','none')
title('End of Trial 60','FontSize',16)
set(gca,'FontSize',16)
box off
ylim([0 12])
xlim([0 0.2]) 
subplot(3,3,3)
bar(linspace(0,0.2,20),I3_s1(70,:),'facecolor','b','edgecolor','none')
title('End of Trial 70','FontSize',16)
set(gca,'FontSize',16)
box off
ylim([0 12])
xlim([0 0.2]) 


subplot(3,3,5)
bar(linspace(0,0.2,20),I3_s2(61,:),'edgecolor','none','facecolor','g')
ylim([0 12])
xlim([0 0.2]) 
title('End of Trial 130','FontSize',16)
set(gca,'FontSize',16)
box off

subplot(3,3,8)
bar(linspace(0,0.2,20),I3_t2(61,:),'edgecolor','none','facecolor','r')
ylim([0 12])
xlim([0 0.2]) 
title('End of Trial 130','FontSize',16)
set(gca,'FontSize',16)
box off

subplot(3,3,6)
hold on
bar(linspace(0,0.2,20),I3_s2(70,:),'edgecolor','none','facecolor','g')
ylim([0 12])
xlim([0 0.2]) 
title('End of Trial 140','FontSize',16)
set(gca,'FontSize',16)
box off

subplot(3,3,9)
bar(linspace(0,0.2,20),I3_t2(70,:),'edgecolor','none','facecolor','r')
ylim([0 12])
xlim([0 0.2]) 
title('End of Trial 130','FontSize',16)
set(gca,'FontSize',16)
box off

suptitle('PC-DCN')
toc